﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HtmlAgilityPack;
using System.Text.RegularExpressions;
using System.Net;
using System.Text;
using System.IO;
namespace VideoGiaitri
{
    public class Uti
    {
        public const string domain = "http://hayhayvuivui.com";
        public static int datetimeidByDay(int days)
        {
            DateTime d1;
            try
            {
                //DateTime st = new DateTime(0001, 1, 1).ToUniversalTime();
                //DateTime now = DateTime.Now.ToUniversalTime();
                //TimeSpan TS = new System.TimeSpan(now.Ticks - st.Ticks);
                //return (int)TS.Days;
                d1 = DateTime.Now.AddDays(days);//.ToUniversalTime();
                string rs = d1.Year.ToString() + d1.Month.ToString("00") + d1.Day.ToString("00");
                return int.Parse(rs);

            }
            catch (Exception ex)
            {
                d1 = DateTime.Now.AddDays(days);//.ToUniversalTime();
                string rs = d1.Year.ToString() + d1.Month.ToString("00") + d1.Day.ToString("00");
                return int.Parse(rs);
            }
            //return DateTime.Now.Year * 365 + DateTime.Now.Month * 30 + DateTime.Now.Day;
        }
        public static string getDiffTimeMinuteFromTwoDate(DateTime date1, DateTime date2)
        {
            try
            {
                DateTime d1 = date1;
                DateTime d2 = date2;
                TimeSpan TS = new System.TimeSpan(d2.Ticks - d1.Ticks);
                int totalHours = (int)Math.Abs(TS.TotalSeconds);
                if (totalHours < 0)
                {
                    return "1 phút trước";
                    //d2 = d2.ToLocalTime();
                    //return d2.Day + "/" + d2.Month + "/" + d2.Year;
                }
                else
                {
                    if (totalHours >= 3600)
                    {
                        int days = totalHours / 3600;
                        return days + " giờ trước";
                    }
                    else return totalHours.ToString() + " giây trước";
                }
            }
            catch (Exception ex)
            {
                return "";
            }
        }
        public static int datetimeid()
        {
            DateTime d1;
            try
            {
                //DateTime st = new DateTime(0001, 1, 1).ToUniversalTime();
                //DateTime now = DateTime.Now.ToUniversalTime();
                //TimeSpan TS = new System.TimeSpan(now.Ticks - st.Ticks);
                //return (int)TS.Days;
                d1 = DateTime.Now;//.ToUniversalTime();
                string rs = d1.Year.ToString() + d1.Month.ToString("00") + d1.Day.ToString("00");
                return int.Parse(rs);

            }
            catch (Exception ex)
            {
                d1 = DateTime.Now;//.ToUniversalTime();
                string rs = d1.Year.ToString() + d1.Month.ToString("00") + d1.Day.ToString("00");
                return int.Parse(rs);
            }
            //return DateTime.Now.Year * 365 + DateTime.Now.Month * 30 + DateTime.Now.Day;
        }
        public static string getFullContentVideo(string mdomain)
        {
            string area1 = "";
            string area2 = "";
           
            if (mdomain.Contains("http://haivl.tv"))
            {

                area1 = "//div[contains(@class,'videoDetails')]";
                area2 = ".//div[contains(@class,'video')]";
               
            }
            if (mdomain.Contains("http://chatvl.com/"))
            {

                area1 = "//div[contains(@id,'divVideoAds')]";
                area2 = ".//center";

            }
            if (mdomain.Contains("http://vntube.tv"))
            {

                area1 = "//div[contains(@id,'content')]";
                area2 = ".//div[contains(@id,'video')]";

            }
            if (mdomain.Contains("http://dechtv.com"))
            {

                area1 = "//div[contains(@class,'menu left')]";
                area2 = ".//div[contains(@class,'box_1')]";

            }
            
            string html;
            var doc = new HtmlDocument();
            //try
            //{
            //    WebClient client = new WebClient();
            //    var data = client.DownloadData(mdomain);
            //    html = Encoding.UTF8.GetString(data);
            //    doc.LoadHtml(html);
            //}
            //catch (Exception ex3) {
                try
                {
                    HtmlWeb hw = new HtmlWeb();
                    doc = hw.Load(mdomain);
                }
                catch (Exception e4) {
                    return "";
                }
            //}
           
            
            

            string fullContent = "";
           
            try
            {
                foreach (HtmlNode AllNode in doc.DocumentNode.SelectNodes(area1))
                {
                    foreach (HtmlNode node1 in AllNode.SelectNodes(area2))
                    {
                        fullContent = node1.InnerHtml;
                        if (!fullContent.Contains("iframe")) return "";
                        return removeAtributeHtml(fullContent);
                    }//foreach 1
                }//for allNode
            }
            catch (Exception ct) { 
            }
            return "";
        }//func
        public static string getFullContentMusic(string mdomain)
        {
            string area1 = "";
            string area2 = "";

            
            if (mdomain.Contains("http://nhacso.net"))
            {

                area1 = "//div[contains(@id,'left_col_1')]";
                area2 = ".//div[contains(@id,'grid_player_flash')]";

            }

            string html;
            var doc = new HtmlDocument();
            //try
            //{
            //    WebClient client = new WebClient();
            //    var data = client.DownloadData(mdomain);
            //    html = Encoding.UTF8.GetString(data);
            //    doc.LoadHtml(html);
            //}
            //catch (Exception ex3) {
            try
            {
                HtmlWeb hw = new HtmlWeb();
                doc = hw.Load(mdomain);
            }
            catch (Exception e4)
            {
                return "";
            }
            //}




            string fullContent = "";

            try
            {
                foreach (HtmlNode AllNode in doc.DocumentNode.SelectNodes(area1))
                {
                    foreach (HtmlNode node1 in AllNode.SelectNodes(area2))
                    {
                        fullContent = node1.InnerHtml;
                        return fullContent;// removeAtributeHtml(fullContent);
                    }//foreach 1
                }//for allNode
            }
            catch (Exception ct)
            {
            }
            return "";
        }//func
        public static string getFullContentGame(string mdomain)
        {
            string area1 = "";
            string area2 = "";


            if (mdomain.Contains("gametv.vn"))
            {

                area1 = "//div[contains(@id,'content')]";
                area2 = ".//div[contains(@id,'video')]";

            }
            else if (mdomain.Contains("chimsedinang"))
            {
                area1 = "//div[contains(@class,'left-body')]";
                area2 = ".//div[contains(@class,'video-embed')]";
            }
            else if (mdomain.Contains("xemgame.com"))
            {
                area1 = "//div[contains(@class,'col-8 main-content')]";
                area2 = ".//div[contains(@class,'featured-vid')]";
            }
            else if (mdomain.Contains("videogame.vn"))
            {
                area1 = "//div[contains(@class,'left-body')]";
                area2 = ".//div[contains(@class,'video-embed')]";
            }
            else if (mdomain.Contains("kenhlmht.com"))
            {
                area1 = "//div[contains(@id,'detail_main_content')]";
                area2 = ".//div[contains(@class,'video-container')]";
            }

            string html;
            var doc = new HtmlDocument();
            //try
            //{
            //    WebClient client = new WebClient();
            //    var data = client.DownloadData(mdomain);
            //    html = Encoding.UTF8.GetString(data);
            //    doc.LoadHtml(html);
            //}
            //catch (Exception ex3) {
            try
            {
                HtmlWeb hw = new HtmlWeb();
                doc = hw.Load(mdomain);
            }
            catch (Exception e4)
            {
                return "";
            }
            //}




            string fullContent = "";

            try
            {
                foreach (HtmlNode AllNode in doc.DocumentNode.SelectNodes(area1))
                {
                    foreach (HtmlNode node1 in AllNode.SelectNodes(area2))
                    {
                        fullContent = node1.InnerHtml;
                        return fullContent;
                    }//foreach 1
                }//for allNode
            }
            catch (Exception ct)
            {
            }
            return "";
        }//func
        public static string removeAtributeHtml(string fullContent)
        {           

            var doc = new HtmlDocument();
            doc.LoadHtml(fullContent);
            var elementsWithStyleAttribute = doc.DocumentNode.SelectNodes("//@style");
            if (elementsWithStyleAttribute != null)
            {
                try
                {
                    foreach (var element in elementsWithStyleAttribute)
                    {

                        element.Attributes["style"].Remove();

                    }
                }
                catch (Exception ex1)
                {
                }
            }
            elementsWithStyleAttribute = doc.DocumentNode.SelectNodes("//@class");
            if (elementsWithStyleAttribute != null)
            {
                try
                {
                    foreach (var element in elementsWithStyleAttribute)
                    {

                        element.Attributes["class"].Remove();

                    }
                }
                catch (Exception ex1)
                {
                }
            }
            elementsWithStyleAttribute = doc.DocumentNode.SelectNodes("//@width");
            if (elementsWithStyleAttribute != null)
            {
                try
                {
                    foreach (var element in elementsWithStyleAttribute)
                    {

                        element.Attributes["width"].Remove();

                    }
                }
                catch (Exception ex1)
                {
                }
            }
            elementsWithStyleAttribute = doc.DocumentNode.SelectNodes("//@face");
            if (elementsWithStyleAttribute != null)
            {
                try
                {
                    foreach (var element in elementsWithStyleAttribute)
                    {

                        element.Attributes["face"].Remove();

                    }
                }
                catch (Exception ex1)
                {
                }
            }
            elementsWithStyleAttribute = doc.DocumentNode.SelectNodes("//@size");
            if (elementsWithStyleAttribute != null)
            {
                try
                {
                    foreach (var element in elementsWithStyleAttribute)
                    {

                        element.Attributes["size"].Remove();

                    }
                }
                catch (Exception ex1)
                {
                }
            }
            fullContent = doc.DocumentNode.InnerHtml;
            fullContent = Regex.Replace(fullContent, "href=\"(.*?)\"", "");
            fullContent = Regex.Replace(fullContent, ">>", "");
            fullContent = Regex.Replace(fullContent, "<h2>", "");
            fullContent = Regex.Replace(fullContent, "</h2>", "");
            fullContent = Regex.Replace(fullContent, "<h1>", "");
            fullContent = Regex.Replace(fullContent, "</h1>", "");
            fullContent = fullContent.Replace("&gt;&gt; ", "");

           


            return fullContent;
        }
        //getCOntent
        public static String getContent(String url)
        {
            String htmlCode = "";
            Random r = new Random();
            string[] myAngent = {"Chrome /36.0.1944.0",
                       "Chrome /35.0.1916.47",
                       "Firefox/29.0",                                              
                       "Chrome/32.0.1667.0",
                       "Chrome/32.0.1664.3",
                       "Chrome/28.0.1467.0",
                       "Chrome/28.0.1467.0",
                       "Firefox /31.0"};
            try
            {
                bool conti = true;
                byte stry = 0;
                WebResponse myResponse;
                StreamReader sr;
                do
                {
                    try
                    {
                        Random random = new Random();
                        int randomNumber = random.Next(0, myAngent.Length);
                        conti = false;
                        HttpWebRequest myRequest = (HttpWebRequest)WebRequest.Create(url);
                        myRequest.Method = "GET";
                        myRequest.Timeout = 15000;
                        myRequest.UserAgent = myAngent[randomNumber];
                        //myRequest.TransferEncoding = "UTF-8";
                        myResponse = myRequest.GetResponse();
                        sr = new StreamReader(myResponse.GetResponseStream(), System.Text.Encoding.UTF8);//.UTF8
                        htmlCode = sr.ReadToEnd();
                        sr.Close();
                        myResponse.Close();

                    }
                    catch (Exception err1)
                    {
                        conti = true;
                        stry++;
                    }
                } while (conti && stry < 3);
            }
            catch (Exception err)
            {
                return "";
            }

            return htmlCode;
        }
        public static string getImageSrc(string content)
        {
            string matchString = Regex.Match(content, "<img.*?data-original=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase).Groups[1].Value;

            return matchString;
        }
    }
}